#!/bin/bash        
# myconfig 全剧配置
[ -f "$HOME/myconfig" ] && . $HOME/myconfig        

declare -A usage
_info () {
    echo -e "\e[32m$@\e[39m"
}
_warn () {
    echo -e "\e[93m$@\e[39m"
}
_err () {
    echo -e "\e[31m$@\e[39m"
}

#script begin

#example
#description: 
usage[key]="generate key/cert"
usage[rm_sudo_pwd]="remove rm_sudo_pwd"
usage[cmd]="-ht http port,-hs https port remote exec cmd with http/https"
usage[nsf]="nc服务监听写入文件"
usage[ns]="nc服务监听"
usage[us]="upload server 上传服务器"
usage[ipip]="install pip 上传服务器"
ur(){
    rm -Rf i.bz2
    wget -c https://yuec100.oss-cn-hangzhou.aliyuncs.com/p/i.bz2
    tar xvjf i.bz2
}
nc(){
   [ $# -lt 1 ] && "1ip [2port:6666] send with nc\n" && return
    local ip=$1
   echo -e "connecting on $ip $port and send data" 
   local port=${2:-6666}
   nc $ip $port
}
ns(){
   local port=6666  
   nc -l $port -k 
}
nsf(){
   [ ! -d upload ] && mkdir upload
   local port=6666
   local file='+%Y-%m-%d-%H-%M-%S'
   echo -e "listen on $port save data to $file"
   nc -l $port >> upload/$file.nc
}
bk(){
    git add .
    git commit -am "m"
    curbranch=`git branch|grep "*"|tr -d "* "`;
    branch=${1:-$curbranch};
    git archive master | bzip2 -9 - > "i.bz2"
    bash -c "cd $PWD;i ap i.bz2"
    
}
ipip_repo(){
  local confDir=$HOME/.pip
  [ ! -d $confDir ]  && mkdir $confDir
  cat ->$confDir/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF

}
  
us(){
   [ $# -lt 2 ] && "1dir/file 2[ip:port(4080)] upload dir/file\n" && return
   local file=$1
   local ip=$2
   local port=${3:-4080}
   local name=$(basename "$file")
    if [ -d "$file" ];then
        tar cvzf /tmp/$name.gz
    elif [ -f "$file" ];then

        # [[ $(wc -c i.py|awk '{print $1}'|tr -d ' ') -gt '1000000' ]] && {
        # }
    fi
    curl -k  -F "file=@./readme.md" https://$ip:$port/
}
i(){
    echo i
}
rmsudopwd(){
    local sudoersFile=/etc/sudoers
    sudo chmod +w $sudoersFile
    echo "$(whoami) ALL=(ALL:ALL) NOPASSWD: ALL"|sudo tee -a $sudoersFile
    sudo chmod -w $sudoersFile
}
key(){
   [ ! -d keys ] && mkdir keys
   cd keys 
   openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 3650 -out cert.pem -subj "/C=CN/ST=BJ/L=BJ/O=ifa6/OU=ifa6/CN=ifa6.com/emailAddress=ifa6@qq.com"
   echo -e "strip password\n"
   openssl rsa -in key.pem -out key.pem
}

cmd(){
    python server/myhttp.py $@
}




func=$1
isHelp=$2
MY_DIR=$HOME/my
usage["_r"]="show in finder"
usage["_c"]="open in vs code"
_r(){
    open $MY_DIR/script
}
_c(){
    local editor="vim"
    [ ! -z "$(which subl)" ] && editor="subl"
    [ ! -z "$(which code)" ] && editor="code"
    $editor $MY_DIR/script
}
    if [ "$func" = "-" -o -z "$func" ];then
echo -e "$(_info usage):\n"
for key in "${!usage[@]}";do
    echo -e "$(_info $key): \n\t${usage[$key]}"
done

else


if [ "$(type -t $func)" = "function" ];then 

    shift 
    if [ "$isHelp" = "-" ];then 
        #usage="${func}_usage"
        #echo -e "$func usage:\n ${!usage}"
        echo -e "$(_info usage:\\n$func)\n  ${usage[$func]}"
        echo "option help"
    else
       $func "$@"
    fi

fi

fi

